Compiling, Assembling, and Linking on UNIX
part 1
- Systems like this encourage the practice of writing a program in many source files
- A major reason for this is that often some of these files will be usable in other programs
- Suppose we start with 3 source files in the C language: X.c, Y.c, and Z.c
- Each .c file goes separately into the C compiler, producing assembly-language files X.s, Y.s, and Z.s
- Each .s file goes separately into the assembler, producing binary files X.o, Y.o, and Z.o
- All of the .o files go together into the relocating linker, producing the executable file a.out
- The compiler and assembler run three times each (once for each input file) and the linker runs once
- On a typical Unix-like system, you type "cc X.c Y.c Z.c", and it's all done automatically
- You can run the executable by typing "a.out" This causes the operating system to
- create a new process for the program
- copy the file's code into that process's memory
- find the program's start address in the file and copy it into the program counter
- there are missing details here that you will get when you take the Operating Systems course
- There are ways that you can type the cc command that will change details of its operation
- For example, you can
- change the name of the output file (cc -o junk)
- begin the process partway through, e.g. cc X.c Y.o Z.s (X.c, Y.o, and Z.s must already exist)
- stop the process partway through
- cc -S X.c Y.c Z.c will stop after creating the .s files
- cc -c X.c Y.c Z.c will stop after creating the .o files
- process a subset of the files (cc -c X.c will produce X.o)
- The naming of the switches is not beautifully consistent; we all have to live with that
- using -o to name the output file is beautifully consistent
- using -S (upper case) instead of -s to stop after generating .s files is not too bad
- using -c to stop after generating the .o files is hugely inconsistent
- but .o is already taken for the output file name
- I think that -c comes from "compile", but assembly is happening as well
- just get used to it
- cc has lots more switches that we don't need to talk about now, if ever
- When they build these systems, they write lots of handy routines that we can use
- A good example is the print function, which is called printf in C
- They use cc -c to compile and assemble all these files into a bunch of .o files
- The .o files are combined into a library file, and each one is individually accessible there
- The linker can look for things in the library if it hasn't found them anywhere else
- This gets us to the next slide